package pc

import (
	"bytes"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"regexp"
	"strings"
)

func GetMainPage(_url string, _client *http.Client) ([]*Sort, error) {
	var err error
	var _rsp string
	var _doc *goquery.Document
	if _rsp, err = GetWeb(_client, _url, nil); err != nil {
		return nil, err
	}
	regexp.Compile("")
	if _doc, err = goquery.NewDocumentFromReader(bytes.NewBufferString(_rsp)); err != nil {
		fmt.Println(err)
		return nil, err
	}
	_sorts := make([]*Sort, 0)
	_doc.Find("ul.nav.navbar-nav>li>a").Each(func(i int, sel *goquery.Selection) {
		_href, ex1 := sel.Attr("href")
		_title, ex2 := sel.Attr("title")
		if ex1 && ex2 && strings.Index(strings.ToLower(_href), "/tv", ) > 0 {
			_sorts = append(_sorts, &Sort{Title: _title, Url: _href, Childs: make([]*Sort, 0)})
		}
		fmt.Println(_href, _title)
	})
	_reg,_:=regexp.Compile(`(?!/tv/)\d+`)
	for _,_s:=range _sorts{
		id:=_reg.FindString(_s.Url)
		fmt.Println("id",id)
	}
	return _sorts, nil
}

func GetSortPage(id, page, order, by, class, year, letter, area, lang string, _sort *Sort)error {
	fmt.Sprintf("m=vod-list-id-%s-pg-%s-order-%s-by-%s-class-%s-year-%s-letter-%s-area-%s-lang-%s.html",
		id, page, order, by, class, year, letter, area, lang,
	)
	return nil
}
